/* 
 * Modbus app for RTU.
 * Copyright (c) 2012  apleilx
 * All rights reserved.
 * no limite
 */

/* ----------------------- Platform includes --------------------------------*/
#define _MBAPP_MODULE_
#include "mbapp.h"
#include "osObjects.h"                                          // RTOS object definitions
#include "DUGSIImaster.h" 
#include "usermanage.h"
#include "pincfg.h"

uint16_t DataRecevie[64];
uint16_t DataSend[64];

uint8_t mbTestErrCnt = 0;
uint8_t mbTestOkCnt = 0;

volatile uint32_t dgs_err;
volatile uint32_t dgs_ok;

void dgus_callback(uint8_t CmdSite, uint8_t ErrSt)
{
    if(ErrSt)
    {
        dgs_err++;
    }
    else
    {
       dgs_ok++; 
    }    
    
}

/*****************************************************************************//*!
* @brief   	Slave device handle.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/   
extern void mbxDataSend(void);
void ModbusTaskEntry (void *argument)
{

    
    user_manage_init();
    
    mbxInit(MB_RTU_MASTER, 115200, MB_PAR_NONE);
    mbxObj.SlaveId=2;
    
    mbStaticCmdSet(&mbxObj,1,0,FUN_CODE_DUGSII_PV_READ,&DataRecevie[16],0,48,dgus_callback);
    mbStaticCmdSet(&mbxObj,2,0,FUN_CODE_DUGSII_PV_WRITE,&DataSend[16],0x2000,48,dgus_callback);
    
    mbStaticCmdSet(&mbxObj,0,0,FUN_CODE_DUGSII_REG_READ,DataRecevie,0x78,16,dgus_callback);
    mbStaticCmdSet(&mbxObj,3,0,FUN_CODE_DUGSII_REG_WRITE,DataSend,0x78,16,dgus_callback);
    
    DataSend[1] = 0x1111;
    DataSend[7] = 0x7777;
    DataSend[16] = 16;
    DataSend[31] = 31;
    DataSend[47] = 47;
    for(;;){
        osDelay(ms_ticks(20));
        
        //mbSlavePoll(&mbyObj);
        //mbSlavePoll(&mbxObj);
        NOP1();
        NOP1();

        DUGSII_MasterPoll(&mbxObj);
    }
}


void mbTestCallBack(uint8_t CmdSite, uint8_t ErrSt){
    if(ErrSt)
        mbTestErrCnt++; 
    else
        mbTestOkCnt++;
}

